<?php

namespace app\controllers;

use yii;
use yii\web\Controller;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class WeiController extends Controller {
        public function actionCheck(){
           $xml=file_get_contents('php://input');
           $xml=simplexml_load_string($xml);
           if ($xml->MsgType == 'text') {
               //用户输入1，回复ok
               if ($xml->Content=='1') {
                   $content="欢迎大美女";
                   $str="<xml>"
                   ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                   ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                   ."<CreateTime>".time()."</CreateTime>"
                   ."<MsgType>text</MsgType>"
                   ."<Content>".$content."</Content><MsgId></MsgId>"  
                   ."</xml>";
                    echo $str;
               }elseif($xml->Content =='2'){
                   $media_id="Rtqrm9ynqimVqRdSAyBc5WVsikZ2q4JjhI04zKWRTuLTAi5yV1FFhsUHO1_YtR_w"; 
                   $str="<xml>"
                    ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                    ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                    ."<CreateTime>".time()."</CreateTime>"
                    ."<MsgType>image</MsgType>"
                    ."<Image><MediaId>".$media_id."</MediaId></Image>"
                    ."</xml>";
                    echo $str;
            }elseif ($xml->Content=='3') {
                   $title="百度一下，你就知道！";
                   $description="度娘，帮你找到你想要的;只需轻轻一点";
                   $picUrl="https://www.baidu.com/img/bd_logo1.png";
                   $url="https://www.baidu.com/";
                   $str="<xml>"
                   ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                   ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                   ."<CreateTime>".time()."</CreateTime>"
                   ."<MsgType>news</MsgType>"
                   ."<ArticleCount>1</ArticleCount>"
                   ."<Articles><item><Title>".$title."</Title> <Description>".$description."</Description><PicUrl>".$picUrl."</PicUrl><Url>".$url."</Url></item></Articles>"
                   ."</xml>";
                   echo $str;  
            }elseif ($xml->Content=='小青蛙') {
                   $title="快乐的一只小青蛙";
                   $description="哈哈哈";
                   $musicURL="http://ting666.yymp3.com:86/new27/qingwa2/13.mp3";
                   $hqmusicUrl="http://ting666.yymp3.com:86/new27/qingwa2/13.mp3";
                   $thumbMediaId="1gjCUaMG0ud8URubkI3ieI1hR1iXCnS7R32mATm1n6pz7Vk3kxbszXbxWZ383ezv";
                   $str="<xml>"
                       ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                       ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                       ."<CreateTime>".time()."</CreateTime>"
                       ."<MsgType>music</MsgType>"
                       ."<Music><Title>".$title."</Title><Description>".$description."</Description><MusicUrl>".$musicURL."</MusicUrl><HQMusicUrl>".$hqmusicUrl."</HQMusicUrl><ThumbMediaId>".$thumbMediaId."</ThumbMediaId></Music>"
                       ."</xml>";
                    echo $str; 
            }elseif ($xml->Content=="小黄鸭") {
              $media_id="kKt-0z923G2Z7Yd3tpPHJoQrkKjOR592cDwvgERE2XHE04zX5OwOo3_1YphOPhaF";
              $title="小黄鸭";
              $description="哈哈哈";
              $str="<xml>"
                   ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                   ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                   ."<CreateTime>".time()."</CreateTime>"
                   ."<MsgType>video</MsgType><Video><MediaId>".$media_id."</MediaId>"
                   ."<Title>".$title."</Title><Description>".$description."</Description></Video>"
                   ."</xml>";
                   echo $str;
            }elseif ($xml->Content=="大美女") {
              $media_id="88iaVDIA18hfrIGfY9cBKhSnUnsGv1vQu2CAO3cCdSze7KkPSeaTmt7uvUFiQMwD";
              $title="大美女";
              $description="棒棒哒";
              $str="<xml>"
                   ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                   ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                   ."<CreateTime>".time()."</CreateTime>"
                   ."<MsgType>video</MsgType><Video><MediaId>".$media_id."</MediaId>"
                   ."<Title>".$title."</Title><Description>".$description."</Description></Video>"
                   ."</xml>";
                   echo $str;
            }                
        }elseif ($xml->Event=='subscribe') {
                   $content="欢迎小哥哥小姐姐，关注我的公众号";
                    $str="<xml>"
                   ."<ToUserName>".$xml->FromUserName."</ToUserName>"
                   ."<FromUserName>".$xml->ToUserName."</FromUserName>"
                   ."<CreateTime>".time()."</CreateTime>"
                   ."<MsgType>text</MsgType>"
                   ."<Content>".$content."</Content>"
                   ."</xml>";
                    echo $str;
           } 
    }
  

    
    public function actionCreatetoken(){
            $appID="wxabf6d9a4d65ec549";
            $appsecret="ceaf9421b684a85e560943881d21bf14";
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appID."&secret={$appsecret}";
            $res=file_get_contents($url);
            $res=json_decode($res,true);
            print_r($res);
        }
}    